/**
 * This file is part of iSecurePasswords.
 *
 *   iSecurePasswords is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   iSecurePasswords is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with iSecurePasswords.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Matthew MacGregor
 * Created: 2012
 * iSecurePasswords, copyright 2013 Sudolink, LLC
 * 
 */ 
package com.sudolink.isp.ui.menu;

import com.sudolink.isp.ui.UICallback;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JMenuItem;
import javax.swing.Timer;

/**
 *
 * @author matsu
 */
public class CopyToClipboard extends JMenuItem implements ClipboardOwner {
    
    private static Timer timer;
    
    private boolean willClearClipboard;
    private UICallback timerUICallback;
    
    
    public CopyToClipboard( final String clipboardText, String menuItemText ){
        super(menuItemText);
        willClearClipboard = true;
        addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setClipboardContents(clipboardText);
                if (willClearClipboard() == true) {
                    if ( timer != null ) {
                        timer.stop();
                    }
                        
                    timer = new Timer(1000, new ActionListener() {
 
                        //TODO:  Add support for making the timeout user preference adjustable.
                        private int timeout = 12;
                        

                        @Override
                        public void actionPerformed(ActionEvent e) {
                            timeout--;
                            if (timeout < 0) {
                                timer.stop();
                                setClipboardContents("");
                                if ( timerUICallback != null ) {
                                    timerUICallback.actionPerformed("Clipboard was cleared");
                                }
                            } else {
                                if (timerUICallback != null) {
                                    timerUICallback.actionPerformed("Clearing the clipboard in " + timeout + " seconds");
                                }
                            }
                        }
                    });
                    timer.setInitialDelay(1000);
                    timer.start();
                }

            }
        });
    }
    
    public CopyToClipboard( String clipboardText )
    {
        this(clipboardText, "Copy To Clipboard");
        
    }
    
    public void stopTimer()
    {
        if (timer != null)
        {
            timer.stop();
        }
    }
    
    /**
     * Place a String on the clipboard, and make this class the owner of the
     * Clipboard contents.
     * @param aString
     */
    public void setClipboardContents(String aString) {
        StringSelection stringSelection = new StringSelection(aString);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents (stringSelection, this);
    }

    @Override
    public void lostOwnership(Clipboard clipboard, Transferable contents) {
        Logger.getLogger(CopyToClipboard.class.getName()).log(Level.INFO, null, "Lost ownership of clipboard.");
    }

    /**
     * @return the willClearClipboard
     */
    public boolean willClearClipboard() {
        return willClearClipboard;
    }

    /**
     * @param willClearClipboard the willClearClipboard to set
     */
    public void willClearClipboard(boolean willClearClipboard) {
        this.willClearClipboard = willClearClipboard;
    }
    
    public void setUICallback( UICallback listener)
    {
        timerUICallback = listener;
    }


    
}

